package com.congwulin.wechat.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.congwulin.wechat.entity.LeftTicket;
import com.congwulin.wechat.entity.Station;
import com.congwulin.wechat.entity.TicketPrice;
import com.congwulin.wechat.mapper.StationMapper;
import com.congwulin.wechat.service.WeChatTrainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

@Controller
@RequestMapping(value = "train")
public class TrainController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WeChatTrainService service;

    @Autowired
    private StationMapper mapper;

    @RequestMapping(value = "/getStation",method = RequestMethod.GET)
    public@ResponseBody
    String getStation(){
        String stationInfo = service.getStationInfo();
//        String stations = stationInfo.substring(21,stationInfo.length()-2);
        String[] stations = stationInfo.substring(21,stationInfo.length()-2).split("@");
//        System.out.println(stations[0]);
        int num = 0;
        for(String station: stations){
            String[] infos = station.split("\\|");
            Station s = new Station();
            s.setCodesimple(infos[0]);
            s.setName(infos[1]);
            s.setCode(infos[2]);
            s.setId(Integer.parseInt(infos[5]));
            mapper.insert(s);
            num++;
        }
        return "add station "+ num;
    }

    @RequestMapping(value = "/getLeftTicket",method = RequestMethod.GET)
    public@ResponseBody
    JSONObject getLeftTicket(String train_date, String from_station, String to_station){
        JSONObject json =  service.getTicketInfo(train_date, from_station, to_station,"ADULT");
        JSONArray result = json.getJSONObject("data").getJSONArray("result");
        List<LeftTicket> list = new ArrayList<LeftTicket>();
        for(int i = 0;i<result.size();i++){
            String ticketinfo = result.getString(i);
            String[] infos = ticketinfo.split("\\|");
            LeftTicket ticket = new LeftTicket();
            ticket.setTrain_no(infos[2]);
            ticket.setTrain_no_simple(infos[3]);
            ticket.setStart_station(infos[4]);
            ticket.setEnd_station(infos[5]);
            List<Station> stationList = mapper.selectByCode(infos[6]);
            ticket.setSearch_start_station(stationList.get(0).getName());
            ticket.setSearch_start_station_telecode(stationList.get(0).getCode());
            stationList = mapper.selectByCode(infos[7]);
            ticket.setSearch_end_station(stationList.get(0).getName());
            ticket.setSearch_end_station_telecode(stationList.get(0).getCode());
            ticket.setSearch_start_time(infos[8]);
            ticket.setSearch_end_time(infos[9]);
            ticket.setTimehour(infos[10].split(":")[0]);
            ticket.setTimeminite(infos[10].split(":")[1]);
            ticket.setStart_time(infos[13]);
            ticket.setStart_no(infos[16]);
            ticket.setEnd_no(infos[17]);
            ticket.setGjrw(infos[21]);
            ticket.setRw(infos[23]);
            ticket.setRz(infos[24]);
            ticket.setWz(infos[26]);
            ticket.setYw(infos[28]);
            ticket.setYz(infos[29]);
            ticket.setEdz(infos[30]);
            ticket.setYdz(infos[31]);
            ticket.setSwz(infos[32]);
            ticket.setDw(infos[33]);
            list.add(ticket);
        }
        json = new JSONObject();
        json.put("ticket",list);
//        String s = (String)result.get(8);
//        String[] infos = s.split("\\|");
//        int i = 0;
//        for(String info: infos){
//            System.out.println("infos["+i+"]:"+info);
//            i++;
//        }
        return json;
    }

    @RequestMapping(value = "/getStationByTrainNo",method = RequestMethod.GET)
    public@ResponseBody
    JSONArray getStationByTrainNo(String train_no, String from_station_telecode, String to_station_telecode, String depart_date){
        JSONObject json =  service.getStationInfoByTrainNo(train_no,from_station_telecode,to_station_telecode,depart_date);
        JSONArray result = json.getJSONObject("data").getJSONArray("data");
        return result;
    }

    @RequestMapping(value = "/getPriceByTrainNo",method = RequestMethod.GET)
    public@ResponseBody
    TicketPrice getPriceByTrainNo(String train_no, String from_station_no, String to_station_no, String train_date){
        JSONObject json =  service.getTicketPrice(train_no,from_station_no,to_station_no,"123469OMP",train_date);
        JSONObject result = json.getJSONObject("data");
        TicketPrice price = result.toJavaObject(TicketPrice.class);
        return price;
    }

    @RequestMapping(value = "/getStationListByName",method = RequestMethod.GET)
    public@ResponseBody
    List<Station> getStationListByName(String name){
        List<Station> list = mapper.selectByName(name);
        return list;
    }
}
